﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using Main_Data;

namespace Category_UI
{
    public partial class PROProfile : CommonForm
    {
        #region Contructors
        public PROProfile()
        {
            InitializeComponent();
        }
        #endregion

        #region Private members
        private int _id = -1;
        #endregion

        #region Events
        private void PROProfile_Load(object sender, EventArgs e)
        {
            try
            {
                pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
                LoadData();
                LoadAuth();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                RefreshModel();
                LoadData();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                CheckAuth("Add");
                dxErrorProvider1.ClearErrors();
                pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
                txtKey.Enabled = true;
                txtKey.Focus();
                _id = -1;
                txtKey.Text = txtName.Text = txtSortOrder.Text = string.Empty;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Edit();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                object cellValue = gridView1.GetFocusedRowCellValue("ID");
                if (cellValue != null)
                {
                    int id = Convert.ToInt32(cellValue);
                    if (CommonConfirm("Bạn muốn xóa dữ liệu này?", "XOA"))
                    {
                        pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
                        txtKey.Text = string.Empty;
                        _id = -1;

                        var obj = Model.PRO_Profile.FirstOrDefault(c => c.ID == id);
                        if (obj != null)
                        {
                            Model.DeleteObject(obj);
                            Model.SaveChanges();
                            CommonMessage("Xóa thành công");
                            LoadData();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
                Point pt = view.GridControl.PointToClient(Control.MousePosition);
                DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);

                if (info.InRow || info.InRowCell)
                {
                    Edit();
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckInput())
                {
                    var obj = Model.PRO_Profile.FirstOrDefault(c => c.ID == _id);
                    if (obj == null)
                    {
                        obj = new PRO_Profile();
                        obj.ID = -1;
                        obj.Code = txtKey.Text;
                        obj.CreatedDate = DateTime.Now;
                        obj.CreatedBy = UserName;
                    }
                    obj.Name = txtName.Text;
                    obj.SortOrder = Convert.ToInt32(txtSortOrder.Text);

                    if (_id == -1)
                        Model.AddToPRO_Profile(obj);
                    Model.SaveChanges();

                    CommonMessage("Cập nhật thành công");
                    pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
                    txtKey.Text = string.Empty;
                    _id = -1;
                    LoadData();
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1;
                txtKey.Text = string.Empty;
                _id = -1;
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
        #endregion

        #region Private members
        private void LoadAuth()
        {
            if (Actions != null)
            {
                if (Actions.Contains("Add"))
                    btnAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                else
                    btnAdd.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

                if (Actions.Contains("Edit"))
                {
                    btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    AcceptButton = btnSave;
                }
                else
                {
                    btnEdit.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    btnSave.Visible = false;
                    AcceptButton = null;
                }

                if (Actions.Contains("Del"))
                    btnDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                else
                    btnDelete.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            }
        }

        private void CheckAuth(string key)
        {
            if (Actions != null && Actions.Contains(key))
            {
                if (key == "Add")
                {
                    btnSave.Visible = true;
                    AcceptButton = btnSave;
                    return;
                }
                else
                {
                    btnSave.Visible = false;
                    AcceptButton = null;
                }

                if (key == "Edit")
                {
                    btnSave.Visible = true;
                    AcceptButton = btnSave;
                    return;
                }
                else
                {
                    btnSave.Visible = false;
                    AcceptButton = null;
                }
            }
        }

        private void LoadData()
        {
            var query = from c in Model.PRO_Profile
                        select new
                        {
                            c.ID,
                            c.Code,
                            c.Name,
                            c.SortOrder
                        };
            gvData.DataSource = query;
        }

        private void Edit()
        {
            object cellValue = gridView1.GetFocusedRowCellValue("ID");
            if (cellValue != null)
            {
                CheckAuth("Edit");
                dxErrorProvider1.ClearErrors();
                pnMain.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Both;
                txtKey.Enabled = false;
                _id = Convert.ToInt32(cellValue);
                var obj = Model.PRO_Profile.FirstOrDefault(c => c.ID == _id);
                if (obj != null)
                {
                    txtKey.Text = obj.Code;
                    txtName.Text = obj.Name;
                    txtSortOrder.Text = obj.SortOrder.ToString();
                }
            }
        }

        private bool CheckInput()
        {
            bool result = true;
            dxErrorProvider1.ClearErrors();
            if (txtKey.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtKey, "Chưa nhập");
                result = false;
            }
            else if (Model.PRO_Profile.FirstOrDefault(c => c.ID != _id && c.Code == txtKey.Text) != null)
            {
                dxErrorProvider1.SetError(txtKey, "Đã sử dụng");
                result = false;
            }
            if (txtName.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtName, "Chưa nhập");
                result = false;
            }
            if (txtSortOrder.Text == string.Empty)
            {
                dxErrorProvider1.SetError(txtSortOrder, "Chưa nhập");
                result = false;
            }
            else
            {
                try
                {
                    int i = Convert.ToInt32(txtSortOrder.Text);
                }
                catch
                {
                    dxErrorProvider1.SetError(txtSortOrder, "Nhập sai");
                    result = false;
                }
            }
            return result;
        }
        #endregion
    }
}
